package uz.edo.app.admin.organizationPages;

import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.CompoundPropertyModel;
import uz.edo.app.admin.AdminPage;
import uz.edo.db.bean.Organisation;
import uz.edo.db.manager.OrganisationManager;

/**
 * Created by IntelliJ IDEA.
 * User: said
 * Date: 1/23/12
 * Time: 2:39 AM
 * To change this template use File | Settings | File Templates.
 */
public class OrganisationListView extends AdminPage{

    private OrganisationManager manager = OrganisationManager.getInstance();

    public OrganisationListView() {
        try {
            final DataView dataView;
            add(dataView = new DataView("organisations",new ListDataProvider(manager.getAll()),ROWS_PER_PAGE){
                @Override
                protected void populateItem(Item item) {
                    final Organisation organisation = (Organisation)item.getModelObject();
                    final CompoundPropertyModel userModel = new CompoundPropertyModel(organisation);
                    item.setModel(userModel);
                    item.add(new MultiLineLabel("nameUz"));
                    item.add(new MultiLineLabel("nameRu"));
                    item.add(new Link("editLink"){
                        @Override
                        public void onClick() {
                            setResponsePage(new OrganisationEdit(organisation));
                        }
                    });
                    item.add(new Link("deleteLink"){
                        @Override
                        public void onClick() {
                            try {
                                manager.delete(organisation);
                                setResponsePage(OrganisationListView.class);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            });

            add(new PagingNavigator("navigator", dataView){
				public boolean isVisible() {
                    return dataView.getPageCount() > 1;
                }
            });

            add(new Link("addLink") {
                @Override
                public void onClick() {
                    goToNew();
                }
            });

            add(new Link("addLink2") {
                @Override
                public void onClick() {
                    goToNew();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void goToNew(){
        setResponsePage(OrganisationAdd.class);
    }
}
